//
//  HotWordsCollectionReusableView.m
//  GiftSay
//
//  Created by QQn1993 on 16/1/11.
//  Copyright © 2016年 liyinkai. All rights reserved.
//

#import "HotWordsCollectionReusableView.h"
#import "HotWordsCollectionViewCell.h"

@implementation HotWordsCollectionReusableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        self.array = [NSArray array];
        self.select = NO;
        [self greatedCollection];
    }
    return self;
}

- (void)greatedCollection
{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    
    flow.minimumInteritemSpacing = CGFLOAT_MIN;
    flow.minimumLineSpacing = CGFLOAT_MIN;
    
    self.collection = [[UICollectionView alloc] initWithFrame:self.frame collectionViewLayout:flow];
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"NightMode"] == YES){
        self.collection.backgroundColor = kNightModeColor;
    }
    else{
        self.collection.backgroundColor = [UIColor whiteColor];
    }
    
    self.collection.delegate = self;
    self.collection.dataSource = self;
    
    //    self.hotwordscollection.hidden = NO;
    
    [self.collection registerClass:[HotWordsCollectionViewCell class] forCellWithReuseIdentifier:@"HotWordsCollectionViewCell"];
    
    [self addSubview:self.collection];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(kWidth / self.array.count, self.frame.size.height);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HotWordsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotWordsCollectionViewCell" forIndexPath:indexPath];
    cell.name = [NSString stringWithFormat:@"%@ ▽",[self.array[indexPath.row] name]];
//    NSLog(@"%@",cell.name);
    cell.select = NO;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HotWordsCollectionViewCell *cell = (HotWordsCollectionViewCell *)[self.collection cellForItemAtIndexPath:indexPath];
    cell.select = !cell.select;
    self.select = cell.select;
    if (cell.select == YES) {
        cell.name = [NSString stringWithFormat:@"%@ △",[self.array[indexPath.row] name]];
    }
    else{
        cell.name = [NSString stringWithFormat:@"%@ ▽",[self.array[indexPath.row] name]];
    }
    
    for (int i = 0 ; i < self.array.count; i ++) {
        if (i != indexPath.row) {
            HotWordsCollectionViewCell *cell = (HotWordsCollectionViewCell *)[self.collection cellForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
            cell.name = [NSString stringWithFormat:@"%@ ▽",[self.array[i] name]];
            cell.select = NO;
        }
    }
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:self.select],@"select",self.array[indexPath.row],@"model", nil];
    
    NSLog(@"************************************");
    
    [self.delegate sendMessage:dic];
    
}

- (void)setArray:(NSArray *)array
{
    if (_array != array) {
        _array = array;
    }
    
    [self.collection reloadData];
    
}

@end
